﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Data.OracleClient;
using ScriptifyLibrary20;

namespace ScriptifyLibrary40
{
    public class ScriptifyOracle : IScriptifyDataAccess
    {
        #region IScriptifyDataAccess Members

        public List<string> GetServers()
        {
            throw new NotImplementedException();
        }

        public List<string> GetDatabases(ScriptifyServer server, IProgressBar bar)
        {
            using (OracleConnection connection = new OracleConnection())
            {
                connection.ConnectionString = GetConnectionString("SSCUSFCMMS02.sscships.silversea.local", "", "mmstest", "mmstest");
                connection.Open();
                Console.WriteLine("State: {0}", connection.State);
                Console.WriteLine("ConnectionString: {0}",
                                  connection.ConnectionString);

                OracleCommand command = connection.CreateCommand();
                string sql = "SELECT * FROM MYTABLE";
                command.CommandText = sql;

                OracleDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    string myField = (string)reader["MYFIELD"];
                    Console.WriteLine(myField);
                }
                return null;
            }
        }

        public List<ScriptifyObject> GetObjects(ScriptifyServer server, IProgressBar bar)
        {
            throw new NotImplementedException();
        }

        public StringBuilder ScriptDatabase(ScriptifyServer server, ScriptifyOptions options, List<ScriptifyObject> objects, IProgressBar bar)
        {
            throw new NotImplementedException();
        }

        #endregion


        /* PRIVATE */
        private static string GetConnectionString(string serverName, string databaseName, string username, string password)
        {
            return  "User Id=" + username + ";Password="+password+";Data Source=(DESCRIPTION=" +
                    "(ADDRESS=(PROTOCOL=TCP)(HOST="+serverName+")(PORT=1521))" +
                    "(CONNECT_DATA=(SID='Fidelio')));";
            return "Server=" + serverName + ";Persist Security Info=True;User ID=" + username + ";Password=" + password + ";Unicode=True"; 
        }

        //private static Server GetServer(ScriptifyServer server)
        //{
        //    if (server.isWindowsAuthentication)
        //        return new Server(server.serverName);
        //    else
        //        return new Server(new ServerConnection(server.serverName, server.username, server.password));
        //}

        private static void ConcatLines(StringCollection strings, StringBuilder sBuilder)
        {
            foreach (string s in strings)
            {
                sBuilder.Append(System.Environment.NewLine + s);

                if (s.StartsWith("SET QUOTED_IDENTIFIER") || s.StartsWith("SET ANSI_NULLS"))
                    sBuilder.Append(System.Environment.NewLine + "GO");
            }


        }

        private static void ConcatGo(StringBuilder sBuilder)
        {
            sBuilder.Append(System.Environment.NewLine + "GO" + System.Environment.NewLine + System.Environment.NewLine);
        }
    }
}
